using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class THOITIET
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public THOITIET(SMS_MO MO)
		{
			WMO = MO;
		}

		public THOITIET()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });

			string strReturnMsg = "";            
			strReturnMsg = GetReturnMsg(msg);
			if(strReturnMsg.Length>160)
			{
				string supertemp="";
				supertemp = strReturnMsg;
				strReturnMsg = strReturnMsg.Substring(0,160);

				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				strReturnMsg = supertemp.Substring(160);

				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				//
				WMT.Insert();
			}
			else
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;          
		}
		public string GetReturnMsg(string[] arr)
		{			
			string msgBody="";
			string supertemp = "";
			for (int i=0; i<arr.Length; i++)
			{				
				supertemp += arr[i];
			}
			string connStr = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
			string strSQL = "select * from Weather where ProvinceCode='" + arr[0] + "' OR ProvinceName='" + arr[0] + "' OR ProvinceName='" + supertemp +"'";
			SqlConnection conn = new SqlConnection(connStr);
			conn.Open();
			SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
			DataSet ds = new DataSet();
			da.Fill(ds);
			conn.Close();
			
			if (WMO.ServiceID.Replace("04","")=="6272" || WMO.UserID=="84984696925")
			{
				if (arr[0]!="")
				{					
					if (ds.Tables.Count>0)
					{
					msgBody= "THOITIET KV:" + ds.Tables[0].Rows[0]["ProvinceName"].ToString() + "\n";
					msgBody += ds.Tables[0].Rows[0]["Description"].ToString();						
					}
					else
					{
						msgBody = "De biet thoi tiet cac vung o VIET NAM hom nay va ngay mai hay soan tin: TT tentinh gui 6272. Vi du soan tin: TT HANOI gui 6272. Chuc cac ban vui ve!";
					}
				}
				else
				{
					msgBody = "De biet thoi tiet cac vung o VIET NAM hom nay va ngay mai hay soan tin: TT tentinh gui 6272. Vi du soan tin: TT HANOI gui 6272. Chuc cac ban vui ve!";
				}
			}
			else
			{
				msgBody = "De biet thoi tiet cac vung o VIET NAM hom nay va ngay mai hay soan tin: TT tentinh gui 6272. Vi du soan tin: TT HANOI gui 6272. Chuc cac ban vui ve!";
			}
			return msgBody;
		}
	}		
}
